﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AnalyticsLyrics
{
    public partial class FrmKaraoke : Form
    {
        private static string uiSep = CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator;
        public FrmKaraoke()
        {
            InitializeComponent();
        }

        private void BT_Open_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            if (open.ShowDialog() == DialogResult.OK)
            {
                TB_File.Text = open.FileName;
                StreamReader reader = new StreamReader(open.FileName);
                TB_Out.Text = "";
                string sOut = "";
                int iType = 0;
                while (reader.Peek() >= 0)
                {
                    string s = reader.ReadLine().Trim();

                    if (s == "[Script Info]")
                    {
                        iType = 1;
                        continue;
                    }

                    if (s == "[V4+ Styles]")
                    {
                        iType = 2;
                    }

                    if (s == "[Events]")
                    {
                        iType = 3;
                    }

                    if (iType == 3)
                    {
                        if (!s.StartsWith("Dialogue:")) continue;
                        if (sOut != "") sOut += ",";
                        sOut += ReadEvent(s);
                    }
                }

                TB_Out.Text = sOut;
            }
        }

        private static string ReadEvent(string s)
        {
            string sOut = "";
            if (s == "") return sOut;
            string[] arr = s.Split(',');
            string sStart = arr[1];
            //string sEnd = arr[2];
            string sText = arr[9];
            if (arr.Length > 9)
            {
                for (int i = 10; i < arr.Length; i++)
                {
                    sText += "," + arr[i];
                }
            }

            double dStartTime = AnalyticsTime(sStart);
            if (sOut != "") sOut += ",";
            sOut += AnalyticsText(sText, dStartTime);
            return sOut;
        }

        private static string AnalyticsText(string s, double dStartTime)
        {
            if (s == "") return "";
            string sOut = "";
            string sSub = "";
            int type = 0, k = 0;
            double subMTime = 0;
            sOut = string.Format("[{0},\"{1}\"]", dStartTime * 1000, "\\n");
            for (int i = 0; i < s.Length; )
            {
                if (i + 2 < s.Length && s[i] == '{' && s[i + 1] == '\\' && (s[i + 2] == 'K' || s[i + 2] == 'k') && type == 0)
                {
                    if (i + 3 < s.Length && (s[i + 3] == 'f' || s[i + 3] == 'F' || s[i + 3] == 'o' || s[i + 3] == 'O' || s[i + 3] == 't' || s[i + 3] == 'T'))
                    {
                        i = i + 4;
                    }
                    else
                    {
                        i = i + 3;
                    }
                    k = i;
                    type = 1;
                }
                else if (s[i] == '}' && type == 1)
                {
                    type = 2;
                    string sSubTime = s.Substring(k, i - k);
                    k = i;
                    subMTime = double.Parse(sSubTime)/100;
                    i++;
                }
                else if (type == 2)
                {
                    if (i == s.Length - 1 || s[i + 1] == '{')
                    {
                        k++;
                        sSub = s.Substring(k, i - k + 1);
                        dStartTime += subMTime;
                        if (sOut != "") sOut += ",";
                        sOut += string.Format("[{0},\"{1}\"]", dStartTime * 1000, sSub);
                        type = 0;
                    }

                    i++;
                }
                else i++;
            }

            return sOut;
        }

        private static double AnalyticsTime(string s)
        {
            string[] arr = s.Split(':');
            double dValue = double.Parse(arr[0]) * 3600 + double.Parse(arr[1]) * 60;
            dValue += uiSep == "," ? double.Parse(arr[2]) : double.Parse(arr[2].Replace('.', ','));
            return dValue;
        }

        private void BT_Save_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.Filter = "JSON|*.json|All file|*.*";
            if (save.ShowDialog() == DialogResult.OK)
            {
                StreamWriter writer = new StreamWriter(save.FileName, false, Encoding.UTF8);
                writer.Write(TB_Out.Text);
                writer.Close();
            }
        }

        private void FrmKaraoke_Load(object sender, EventArgs e)
        {
        }
    }
}
